package zkk.com.mengqu.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import zkk.com.mengqu.R;
import zkk.com.mengqu.adapter.AddressAdapter;
import zkk.com.mengqu.manager.APIManager;
import zkk.com.mengqu.models.Address;
import zkk.com.mengqu.simplepreferences.Share;
import zkk.com.mengqu.util.FinalString;

/**
 * Created by 40955 on 2017/8/21.
 */

public class AddressActivity extends Activity implements View.OnClickListener{

    private Context context;
    private ImageView back;
    private TextView title;//标题
    private SwipeRefreshLayout refresh;//下拉刷新
    private ListView listView;
    private AddressAdapter adapter;

    private int type;//类型,
    private List<Address> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        type = getIntent().getIntExtra(FinalString.ADDRESS_TYPE,-1);
        Log.w(FinalString.ADDRESS_TYPE, "" + type);
        initView();

    }

    public void initView(){
        context = getApplicationContext();
        title = (TextView)findViewById(R.id.tx_title);
        back = (ImageView)findViewById(R.id.iv_back);

        refresh = (SwipeRefreshLayout)findViewById(R.id.shelf_swipe_refresh);//下拉刷新
        refresh.setColorSchemeColors(Color.GREEN, Color.RED, Color.YELLOW);//下拉刷新颜色
        listView = (ListView)findViewById(R.id.lv_list);
        adapter = new AddressAdapter(context, list);
        listView.setAdapter(adapter);

        title.setText(type == 1 ? "请选择地址" : "我的地址");

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh.setRefreshing(true);
                getAddress();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (type == FinalString.SHOW_ADDRESS){
                    Intent intent = new Intent(AddressActivity.this, AddressDetailActivity.class);
                    intent.putExtra(FinalString.ADDRESS_DETAIL_TYPE, FinalString.MODIFY_ADDRESS_DETAIL);
                    intent.putExtra(FinalString.ADDRESS_ID, list.get(i).getId());
                    intent.putExtra(FinalString.ADDRESS_UID, list.get(i).getUid());
                    startActivity(intent);
                }else {
                    Intent intent = new Intent();
                    intent.putExtra(FinalString.ADDRESS_NAME, list.get(i).getName());
                    intent.putExtra(FinalString.ADDRESS_SEX, list.get(i).getSex());
                    intent.putExtra(FinalString.ADDRESS_PHONE, list.get(i).getPhone());
                    intent.putExtra(FinalString.ADDRESS, list.get(i).getAddress());
                    setResult(FinalString.CHOOSE_ADDRESS, intent);
                    finish();
                }

            }
        });


        back.setOnClickListener(this);
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
//        getAddress();
//    }

    //这个Activity得到或者失去焦点的时候
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        refresh.setRefreshing(true);
        getAddress();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }

    public void getAddress(){
        list.clear();
        int uid = Integer.parseInt(Share.getUserId());
        APIManager.setManager()
                .getAddresses(uid)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Address>>() {
                    @Override
                    public void onCompleted() {
                        Log.w("onCompleted-getAddress" , "onCompleted");
                        refresh.setRefreshing(false);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.w("onError-getAddress" , "onError");
                        e.printStackTrace();
                        refresh.setRefreshing(false);
                    }

                    @Override
                    public void onNext(List<Address> addresses) {
                        Log.w("getAddress" , "" + addresses);
                        list.addAll(addresses);
                        adapter.notifyDataSetChanged();
                    }
                });
    }


}
